package app.musicPlayer.domain;

import java.util.ArrayList;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import app.persistencia.CancionLista;
import app.utils.Utiles;

public class SelectFromAlbum extends ListActivity {

	String albumSeleccionado;
	ArrayAdapter<Model> adapter;
	ArrayList<Model> arrayAlbum;

	
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		arrayAlbum = new ArrayList<Model>();
		ArrayList<CancionLista> canciones = CancionesSistema.getIntances().getTodasLasCanciones();
		for(int i = 0; i < canciones.size(); i++)
		{
			String album = canciones.get(i).getAlbum();
			int j = estaEnLaLista(album);
			System.out.println(album);
			if(j == -1)
				arrayAlbum.add(new Model(album, 1,"Cancion(es)"));
			else
				arrayAlbum.get(j).incrementar();
		}
		
		adapter = new InteractiveArrayAdapterArrayString(
				SelectFromAlbum.this,arrayAlbum);
		// Create an array of Strings, that will be put to our ListActivity
		setListAdapter(adapter);
		final ListView listsview = getListView();
		listsview.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v, int posicion, long id) {
            	albumSeleccionado = arrayAlbum.get(posicion).getName();
            	goSongs();
            }
        });
	}

	private int estaEnLaLista(String album) {
		boolean b = false;
		int value = -1;
		for(int i = 0; i < arrayAlbum.size() && !b; i++){
			if (arrayAlbum.get(i).getName().equals(album)) {
				b = true;
				value = i;
			}
		}
		return value;
	}

	protected void goSongs() {
		Intent intent = new Intent(SelectFromAlbum.this, AllSongs.class);
    	Bundle b = new Bundle();
		b.putString(Utiles.FACTORDESELECCION, String.valueOf(Utiles.SELECT_FROM_ALBUM));
		b.putString(Utiles.VALOR, albumSeleccionado);
		intent.putExtras(b);
    	finish();
    	startActivity(intent);
	}

	public boolean onKeyDown(int keyCode, KeyEvent event) {
	    if (keyCode == KeyEvent.KEYCODE_BACK) {
	        //moveTaskToBack(true);          
	    	returnToViewMain(null);
	    	return true;
	    }
	    return super.onKeyDown(keyCode, event);
	}
	private void returnToViewMain(Object object) {
    	Intent intent = new Intent(SelectFromAlbum.this, SocialMusicPlayerActivity.class);
    	finish();
    	startActivity(intent);
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
	    MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.menu.viewsongsmenu3, menu);
	    return true;
	}
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
	    switch (item.getItemId()) {
	        case R.id.Volver :
	        	returnToViewMain(null);
	        	return true;
	       default :
	            return super.onOptionsItemSelected(item);
	    }
	}

	private void seleccion() {
		Utiles.showMessage(SelectFromAlbum.this, "Proximamente");
	}
}
